package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.service.ChartsService;
import cn.zyy.hotel.vo.ChartsVO;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/admin/charts")
public class ChartsController {

    @Resource
    private ChartsService chartsService;

    //统计每年营业额
    @RequestMapping("/getHotelYearCharts")
    public Object getHotelYearCharts(){
        //创建map集合保存数据
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //调用统计每个年度营业总额的方法
        List<Map> mapList = chartsService.getTotalPriceByYear();
        //创建两个List集合，分别保存年份以及对应的营业额
        ArrayList<String> yearsList = new ArrayList<String>();
        ArrayList<Double> yearPrice = new ArrayList<Double>();
        //循环遍历每年营业额集合
        for (Map map : mapList) {
            //年份
            yearsList.add(map.get("years").toString());
            //营业额
            yearPrice.add(Double.valueOf(map.get("totalPrice").toString()));
        }
        dataMap.put("yearsList",yearsList);
        dataMap.put("yearPrice",yearPrice);
        return dataMap;
    }

    //统计月营业额报表
    @RequestMapping("/getHotelYearOfMonthCharts")
    public Object getHotelYearOfMonthCharts(String year){
        //调用查询每年每月营业额的方法
        return chartsService.getTotalPriceByYearOfMonth(year);
    }

    //统计季度营业额报表
    @RequestMapping("/getHotelQuarterCharts")
    public Object getHotelQuarterCharts(String year){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用根据指定的年份查询季度营业额报表的方法
        List<Map> list = chartsService.getQuarterTotalPriceByYear(year);
        //创建两个集合，分别保存季度名称和季度营业额
        //保存季度名称
        ArrayList<String> key = new ArrayList<String>();
        //保存季度营业额
        ArrayList<ChartsVO> chartsVOS = new ArrayList<ChartsVO>();
        //循环遍历
        for (Map m : list) {
            //获取每个季度的key
            String typeName = m.get("typeName").toString();
            key.add(typeName);
            //创建数组，保存每个季度的营业额
            Double[] dataList = {Double.valueOf(m.get("Q1").toString()),Double.valueOf(m.get("Q2").toString()),Double.valueOf(m.get("Q3").toString()),Double.valueOf(m.get("Q4").toString())};
            //创建ChartSVO类保存数据
            ChartsVO chartsVO = ChartsVO.builder().name(typeName).type("line").stack("总营业额").data(dataList).build();
            //将对象添加到营业额集合
            chartsVOS.add(chartsVO);
        }
        map.put("keyList",key);
        map.put("valueList",chartsVOS);
        return map;
    }

    //统计每个房型每年每月被预定的数量
    @RequestMapping("/getHotelChartsByTypeName")
    public Object getHotelChartsByTypeName(String date){
        //判断日期是否为空
        if (StringUtils.isEmpty(date)){
            //为空则格式化当前日期
            date = new SimpleDateFormat("yyyy-MM").format(new Date());
        }
        return chartsService.getRoomTypeCountByYearAndMonth(date);
    }

    //统计当前开房报表
    @RequestMapping("/getCurrentDateHotelCharts")
    public Object getCurrentDateHotelCharts(String date){
        //判断日期是否为空
        if (StringUtils.isEmpty(date)){
            //为空则格式化当前日期
            date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        return chartsService.getCurrentDateHotelByDate(date);
    }

    //统计房型销量
    @RequestMapping("/getRoomTypeList")
    public Object getRoomTypeList(){
        return chartsService.getRoomTypeList();
    }

}
